using System;
using BRozklad.DBLib.DataModel;

namespace BRozklad.RozkladLib
{
    public class HTMLDrawing : IDrawing
    {
        public string Draw()
        {
            ReaderFullResult reader = new ReaderFullResult();
            Array fullResult = reader.readFullResult();
            string res = "";
            res += "<table border=1>\n";
            for (int i = 0; i < fullResult.GetLength(0); i++)
            {
                res += "<tr>\n";
                for (int j = 0; j < fullResult.GetLength(1); j++)
                {
                    if (fullResult.GetValue(i, j) != null &&
                        fullResult.GetValue(i, j).GetType().ToString() == "System.Boolean")
                    {
                        continue;
                    }
                    if (fullResult.GetValue(i, j) != null &&
                        fullResult.GetValue(i, j).GetType().ToString() == "System.Double")
                    {
                        res += "<td>.</td>";
                        continue;
                    }
                    string smallRes = "";
                    int rows = 1;
                    int columns = 1;
                    if (fullResult.GetValue(i, j) != null &&
                        fullResult.GetValue(i, j).GetType().ToString() == "System.String" &&
                        i != 0)
                    {
                        rows = 2;
                        fullResult.SetValue(false, i + 1, j);

                    }
                    if (fullResult.GetValue(i, j) != null)
                    {
                        if (fullResult.GetValue(i, j).GetType().ToString() == "System.String")
                        {
                            smallRes += fullResult.GetValue(i, j).ToString();
                        }
                        else
                        {
                            DrawingDataRow row = reader.getInfoForPair((int)(fullResult.GetValue(i, j)));
                            int k = j + 1;
                            //object ttt1 = fullResult.GetValue(i, j);
                            //object ttt2 = fullResult.GetValue(i, k);
                            int curHash = hashPair(reader.getInfoForPair((int)(fullResult.GetValue(i, j))));
                            while (k < fullResult.GetLength(1))
                            {
                                if (fullResult.GetValue(i, k) == null)
                                    break;
                                if (fullResult.GetValue(i, k).GetType().ToString() != "System.Int32")
                                    break;
                                int nextHash = hashPair(reader.getInfoForPair((int)(fullResult.GetValue(i, k))));
                                if (curHash < 0 || nextHash < 0 || curHash != nextHash)
                                    break;
                                k++;
                            }
                            columns = k - j;
                            k = i + 1;
                            while (k < fullResult.GetLength(0))
                            {
                                if (fullResult.GetValue(k, j) == null)
                                    break;
                                if (fullResult.GetValue(k, j).GetType().ToString() != "System.Int32")
                                    break;
                                int nextHash = hashPair(reader.getInfoForPair((int)(fullResult.GetValue(k, j))));
                                if (curHash < 0 || nextHash < 0 || curHash != nextHash)
                                    break;
                                k++;
                            }
                            rows = k - i;
                            if (row != null && rows == 1)
                            {
                                for (int j1 = j; j1 < j + columns; j1++)
                                {
                                    if ((i+1 < fullResult.GetLength(0)) && fullResult.GetValue(i+1, j1) == null)
                                        fullResult.SetValue(0.0, i+1, j1);
                                }
                            }
                            for (int i1 = i; i1 < i+rows; i1++)
                            {
                                for (int j1 = j; j1 <j+columns; j1++)
                                {
                                    fullResult.SetValue(false, i1, j1);
                                }
                            }
                            if (row != null)
                                smallRes += getInfo(row);
                        }
                    }
                    res += "<td ";
                    if (rows != 1)
                        res += "rowspan=" + rows.ToString() + " ";
                    if (columns != 1)
                        res += "colspan=" + columns.ToString();
                    res += ">";
                    res += smallRes;
                    res += "</td>";
                }
                res += "\n";
                res += "</tr>\n";
            }
            res += "</table>\n";
            return res;
        }

        string getInfo(DrawingDataRow row)
        {
            string res = "";
            res += row.subjectSubject;
            res += "<br>";
            res += "<b>" + row.teacherName + "</b>";
            res += "<br>";
            res += "<i>" + row.auditoriasName + "</i>";
            return res;
        }

        int hashPair(DrawingDataRow row)
        {
            if (row == null)
                return - 1;
            return row.paraID;
        }
    }
}
